package bsk.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import komunikaty.Upowazniony;
import bsk.Connect;

public class Uwierzytelnienie extends JPanel {
	private JTextField tfNazwisko;
	private JTextField tfPesel;
	private JTextField tfImie;
	
	//public JPanel contentPane = new JPanel();
	private JLabel lblKomunikat;
	private JPanel panelMenu;

	public Uwierzytelnienie(JLabel lblKonumitat, JPanel pMenu) {
		this.lblKomunikat = lblKonumitat;
		this.panelMenu = pMenu;
		init();
		
	}
	
	public void init() {
		
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setLayout(null);
		setSize(477, 361);
		
		JLabel lbUwierzytelnienie = new JLabel("Uwierzytelnianie");
		lbUwierzytelnienie.setBounds(10, 11, 132, 20);
		add(lbUwierzytelnienie);
		lbUwierzytelnienie.setFont(new Font("Times New Roman", Font.BOLD, 15));
		
		tfImie = new JTextField();
		tfImie.setBounds(187, 42, 170, 20);
		add(tfImie);
		tfImie.setColumns(10);
		
		JLabel lbImie = new JLabel("Imie :");
		lbImie.setBounds(143, 44, 34, 14);
		add(lbImie);
		lbImie.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		tfNazwisko = new JTextField();
		tfNazwisko.setBounds(187, 73, 170, 20);
		add(tfNazwisko);
		tfNazwisko.setColumns(10);
		
		JLabel lbNazwisko = new JLabel("Nazwisko :");
		lbNazwisko.setBounds(115, 74, 62, 16);
		add(lbNazwisko);
		lbNazwisko.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JLabel lbPesel = new JLabel("PESEL :");
		lbPesel.setAlignmentX(Component.CENTER_ALIGNMENT);
		lbPesel.setBounds(133, 105, 44, 16);
		add(lbPesel);
		lbPesel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		tfPesel = new JTextField();
		tfPesel.setBounds(187, 104, 170, 20);
		add(tfPesel);
		tfPesel.setColumns(10);
		
		JButton button = new JButton("Uwierzytelnij");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(validacja()) {
					
					Upowazniony U_out = new Upowazniony(tfImie.getText(), tfNazwisko.getText(), new BigInteger(tfPesel.getText()));
			        Connect con = new Connect("servers.jks", "098765", true, false, false, false, U_out, 0);
			        con.run();
					
					//con.setClientData(tfImie.getText(), tfNazwisko.getText(), tfPesel.getText());
					
					lblKomunikat.setText(con.getMessageServer());
					setVisible(false);
					panelMenu.setVisible(true);
				}
				
			}
		});
		
		button.setBounds(225, 157, 132, 23);
		add(button);
		setVisible(true);
	}
	
	private boolean validacja() {
		boolean flaga = true;
		
		try {
//			if(tfPesel.getText().length()!=11) {
//				flaga = false;
//				throw new Exception();
//			}
			Integer.parseInt(tfPesel.getText());
			tfPesel.setBorder(UIManager.getBorder("ComboBox.border"));
		}catch (Exception e) {
			tfPesel.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(255, 0, 0)));
			flaga = false;
		}
		
		if(tfImie.getText().length()<2) {
			tfImie.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(255, 0, 0)));
			flaga = false;
		} else {
				tfImie.setBorder(UIManager.getBorder("ComboBox.border"));
				}
		
		if(tfNazwisko.getText().length()<2) {
			tfNazwisko.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(255, 0, 0)));
			flaga = false;
		} else {
				tfNazwisko.setBorder(UIManager.getBorder("ComboBox.border"));
				}
		
		return flaga;
	}

//	public JPanel getPanel() {
//		return contentPane;
//	}


}
